﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FrienDev.Data;

namespace FrienDev.BusinessRules
{
    public class ApplicationRules
    {
        public static ApplicationRules Instance = new ApplicationRules();

        private FrienDevDataContext m_DataContext = FrienDevDataContext.Instance;

        private ApplicationRules()
        {
        }

        public IEnumerable<Application> GetAllDefaultApplication()
        {
            IEnumerable<Application> applications = from app in m_DataContext.Applications
                                                    where app.IsDefault == true
                                                    select app;
            return applications;
        }

        public IEnumerable<Application> GetApplicationsByUserId(string userId)
        {
            IEnumerable<Application> applications = from app in m_DataContext.UserApplications
                                                    where app.UserId == userId && app.Application.IsPublic == true
                                                    select app.Application;
            return applications;
        }

        public UserApplication GetCurrentUserApplicationByUserIdAndType(string userId, Type source)
        {
            IEnumerable<UserApplication> userApplication = from userApp in m_DataContext.UserApplications
                                                           where userApp.UserId == userId
                                                           && userApp.Application.AssemblyName == source.Assembly.FullName
                                                           && userApp.Application.ClassName == source.FullName
                                                           select userApp;
            return userApplication.SingleOrDefault();
        }
    }
}
